<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>订单管理管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			var usesssLength=$("#usesssLength").val();
			chooseVip();
			var st=$("#userId").val();
			var pst=$("#usps").val();
			var mess=$("#mess").val();
			
			if(mess==1){
				showTip("模板格式不正确!", "", 3000, 0);
			}
			if(pst>0){
				showTip("代下单成功!", "", 3000, 0);
			}
			if($.trim(st).length>0){
				$("#contentTable").removeAttr("style");
				$("#formactions").removeAttr("style");
				
			}
			$("#btnImport").click(function(){
				$.jBox($("#importBox").html(), {title:"导入数据", buttons:{"关闭":true}, 
					bottomText:"导入文件不能超过5M，仅允许导入“xls”或“xlsx”格式文件！"});
			});
			
			var gs=$("#successGoods").val();
			/* if($.trim(gs).length>0){
				$("#btnCancel").attr("disabled", 'disabled');
			} */
			
			if(usesssLength<1){
				
				$("#btnCancel").hide();
			}
			var ppType=${ppType}
		   console.log(typeof(ppType)== "undefined")
			if (typeof(ppType)!= "undefined"){
				if(ppType==0){
					$("#s2id_ppType").find(".select2-chosen").text("配件")
					$(".pipei").val(0);
				}
				if(ppType==1){
					$("#s2id_ppType").find(".select2-chosen").text("厂家")
					$(".pipei").val(1);
				}
				
				if(ppType==2){
					$("#s2id_ppType").find(".select2-chosen").text("物料")
					$(".pipei").val(2);
				}
			}
		})
		
		function chooseVip(){
			
			var storeCode=$("#buyStoreCode").val();
			var usetId=$("#userId").val();
			$.ajax({
				type:"post",
				url : "${ctx}/sys/orders/userList",
				data:{"storeCode":storeCode},
				success:function(msg){
				  $("#userIdss").empty(); 
				  if(msg.length <=0){
					  $('#userIdss').append('<option selected="selected" value="">无数据</option>')   
				  }
	 	             
		            if (msg.length > 0) {
		            	$('#userIdss').append('<option selected="selected" value="">代下单会员</option>') 
		                for (var i = 0; i < msg.length; i++) {
		                        var partId = msg[i].id;
		                        var partName = msg[i].company;
		                         
		                        if(usetId==partId){
		             		       
			                    	   var $option = $("<option selected='selected'>").attr({
				                            "value" : partId
				                        }).text(partName);
			                    }else{ 
		       
		                        var $option = $("<option>").attr({
		                            "value" : partId
		                        }).text(partName);
		                        
			                    } 
		                        $("#userIdss").append($option);
		                }
		                $("#userIdss").change();

		            }
				}	
			})
		}
		function yzCode(){
			var storeCode=$("#buyStoreCode").val();
			var userId=$("#userIdss").val();
		
			var pid=$(".pipei").val();
			if(storeCode.length>0&&userId.length>0&&pid.length>0){
				$("#btnImport").removeAttr("disabled");
			}else{
				$("#btnImport").attr("disabled", 'disabled');
			}
		}
		
		function storCode(){
			var storeCode=$("#buyStoreCode").val();
			console.log(storeCode)
			$("#storeCrr").attr('value',storeCode);
			
		}
		
		function usssId(){
			var userId=$("#userIdss").val();
			//$("#uId").val(userId);
			$("#uId").attr('value',userId);
		}
		
		function pType(){
			var pid=$("#ppType").val();
		    $(".pipei").val(pid);
			
		}
		
		
		function subsssstt(e){
			var uploadFile=$(e)[0].files[0].size;
			if(uploadFile>0){
				$(e).parent().find(".btnImportSubmit").removeAttr("disabled");
			}else{
				$(e).parent().find(".btnImportSubmit").attr("disabled", 'disabled');
			}
		}
		
		function xiaMu(){
			window.location.href="${ctx}/sys/orders/exportReplaceOrder"
		}
		
		/* function deleteGoods(e){
			var st=$(e).parent().parent().find("td").eq(1).val();
			alert(st);
			$(e).parent().parent().remove();
		} */
		
		function daoChu(){
			
			window.location.href="${ctx}/sys/orders/exportOrders";
		}
				
		function showOrders(){
			var userId=$("#uId").val();
			var storeCode=$("#storeCrr").val();
			console.log(storeCode)
			 window.location.href="${ctx}/sys/orders/replaceShow?userId="+userId+"&storeCode="+storeCode; 
			
		}		
	</script>
</head>
<body>
  <input type="hidden" value="${mess }" id="mess"/>
  <input type="hidden" value="${ps}" id="usps">
  <input type="hidden" value="${usesssLength}" id="usesssLength">
   <div id="importBox" class="hide">
		<form id="importForm" action="${ctx}/sys/orders/importOrders" method="post" enctype="multipart/form-data"
			class="form-search" style="padding-left:20px;text-align:center;" ><br/>
			<input id="uploadFile" name="file"  type="file" style="width:330px" onchange="subsssstt(this)"/><br/><br/>
			<input id="storeCrr" value='${storeCode}' name="storeCode" type="hidden"/>
			<input id="uId" value='${userId }'  name="useName" type="hidden" />　
			<input name="ppType" class="pipei" type="hidden" value="0">　
			<input id='btnImportSubmit' class="btn btn-primary btnImportSubmit" type="submit" value="导    入   " disabled="disabled"/>
		</form>
	</div>
	<input value="${successGoods }" id="successGoods" type="hidden"/>
	
   <input value="${userId }" id="userId" type="hidden"/>
	<ul class="nav nav-tabs">
		<li class="active"><a href="javascript:void(0)">代下单</a></li>
		
	</ul>
	   <form:form id="searchForm" modelAttribute="orders" action="${ctx}/sys/orders/" method="post" class="breadcrumb form-search">
		<ul class="ul-form">
			<li><label>下单门店：</label><form:select id="buyStoreCode" path="buyStoreCode" class="input-medium" onchange="yzCode();chooseVip();storCode()">
			     <form:option value="" label="下单门店"/> 
			     <form:options items="${store}" htmlEscape="false" itemValue="code" itemLabel="cnName"/> 
			</form:select>
			</li>
			<li><label>代下单会员：</label><form:select id="userIdss" path="userId" class="input-medium" onchange="yzCode();usssId()" >
			    <form:option value="" label="代下单会员"/> 
			   <%--  <form:options items="${store}" htmlEscape="false" itemValue="code" itemLabel="cnName"/> --%>
			</form:select>
			</li>
			
			<li><label>匹配方式：</label>
			<form:select id="ppType" path="ppType" class="input-medium" onchange="pType();yzCode()">
			    <form:option value="0" label="配件编号"/> 
			    <form:option value="1" label="厂家编号"/> 
			    <form:option value="2" label="物料编号"/> 
			</form:select>
			</li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="button" value="下载模板" onclick="xiaMu()"/></li>
			<li class="btns"><input id="btnImport" class="btn btn-primary" type="button" value="批量上传" disabled="disabled"/></li>
			<li class="clearfix"></li>
		</ul>
     </form:form>
	<table id="contentTable" class="table table-striped table-bordered table-condensed" style="display:none">
		<thead>
			<tr>
				<th>序号</th>
				<th>配件编号</th>
				<th>商品名称</th>
				<th>单价</th>
				<th>数量</th>
				<th>小计</th>
				<th>状态</th>
				<shiro:hasPermission name="sys:orders:edit"><th>操作</th></shiro:hasPermission>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${list}" var="list" varStatus="status"> 
			 <tr>
			    <td>${status.index+1 }</td>
			    <td>${list.partsCode }</td>
				<td>${list.cnName }</td>
				<td>${list.price }</td>
			    <td>${list.goodsNum }</td>
				<td>${list.totalMoney }</td>
				<td>
				 <c:if test="${list.failReason==0 }">
				   成功
				</c:if>
				<c:if test="${list.failReason==1 }">
				   配件编号匹配失败
				</c:if>
				<c:if test="${list.failReason==2 }">
				   商品名称匹配失败
				</c:if>
				<c:if test="${list.failReason==3 }">
				   配件编号与商品名称不一致
				</c:if>
				</td>	
				<shiro:hasPermission name="sys:orders:edit"><td>
				<%-- <c:if test="${list.failReason!=0 }"> --%>
				<a href="${ctx}/sys/orders/delectOrder?goodsName=${list.cnName}&userId=${userId}&storeCode=${storeCode}&successGoods=${successGoods}" >删除</a>
				<%-- </c:if> --%>
				</td></shiro:hasPermission>
			</tr>
		 </c:forEach> 
		</tbody>
	</table>
	<div class="form-actions" style="display:none" id="formactions">
			<shiro:hasPermission name="sys:area:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="导出"  onclick="daoChu()"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn nextsss" type="button" value="下一步" onclick="showOrders()"/>
	</div>
</body>
</html>